﻿using AutoMapper.Internal.Mappers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Himp.TaskScheduling
{
    /// <summary>
    /// 参数配置
    /// </summary>
    public class ParamConfigService : CrudAppService<ParamConfig, ParamConfigDto, long, ParamConfigInput, ParamConfigCreateOrUpdateDto, ParamConfigCreateOrUpdateDto>, IParamConfigService
    {
        private IRepository<ParamConfig, long> _paramConfigRepository;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ParamConfigRepository"></param>
        public ParamConfigService(IRepository<ParamConfig, long> paramConfigRepository) : base(paramConfigRepository)
        {
            _paramConfigRepository = paramConfigRepository;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public override async Task<PagedResultDto<ParamConfigDto>> GetListAsync(ParamConfigInput input)
        {
            var iQuery = await _paramConfigRepository.GetQueryableAsync();
            var allList = iQuery.ToList();
            allList = allList.Where(a => a.ParentId == null).ToList();

            if (!string.IsNullOrEmpty(input.Filter))
            {
                allList = allList.Where(a => a.ParamName.Contains(input.Filter)
                || (a.Children!=null && a.Children.Any(aa => aa.ParamName.Contains(input.Filter)
                || (aa.Children!=null&& aa.Children.Any(aaa => aaa.ParamName.Contains(input.Filter)))))
                ).ToList();
            }

            if (input.Pi == 0)
            {
                var itemsDto = ObjectMapper.Map<List<ParamConfig>, List<ParamConfigDto>>(allList);

                return new PagedResultDto<ParamConfigDto>(itemsDto.Count, itemsDto);
            }
            else
            {
                var totalCount = allList.Count();
                var items = allList.Skip((input.Pi - 1) * input.Ps).Take(input.Ps);
                var itemsDto = ObjectMapper.Map<List<ParamConfig>, List<ParamConfigDto>>(items.ToList());

                return new PagedResultDto<ParamConfigDto>(totalCount, itemsDto);
            }
        }

        public override Task<ParamConfigDto> CreateAsync(ParamConfigCreateOrUpdateDto input)
        {
            if (!string.IsNullOrEmpty(input.KeyPath) && !string.IsNullOrEmpty(input.PropertyKey))
            {
                RedisHelper.SetString($"TopicKeyPath:{input.PropertyKey}", input.KeyPath);
            }

            return base.CreateAsync(input);
        }

        public override Task<ParamConfigDto> UpdateAsync(long id, ParamConfigCreateOrUpdateDto input)
        {
            if (!string.IsNullOrEmpty(input.KeyPath) && !string.IsNullOrEmpty(input.PropertyKey))
            {
                RedisHelper.SetString($"TopicKeyPath:{input.PropertyKey}", input.KeyPath);
            }

            return base.UpdateAsync(id, input);
        }
    }
}
